﻿using Simple.BasicNet.Core.Configuration;
using Simple.BasicNet.Core.Handle;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace App.Cache
{
    public class RedisClientService : IRedisClientService
    {
        private const string KeySeparator = ":";
        public RedisClientService(IConfiguration configution)
        {
            CSRedis.CSRedisClient csredis;
			//if (redisStoreOption.RedisConnectionStrings.Count == 1)
			//{
			//    //普通模式

			  csredis = new CSRedis.CSRedisClient(configution.GetConfiguration<IDTConfiguration>().RedisConnection);
            //}
            //else
            //{
            //    //集群模式
            //    //实现思路：根据key.GetHashCode() % 节点总数量，确定连向的节点
            //    //也可以自定义规则(第一个参数设置)
            //    csredis = new CSRedis.CSRedisClient(null, redisStoreOption.RedisConnectionStrings.ToArray());
            //}
            //初始化 RedisHelper
            RedisHelper.Initialization(csredis);
        }

        public async Task<T> GetAsync<T>(string key)
        {
            var result = await RedisHelper.GetAsync<T>(key);
            return result;
        }
        public async Task<int> CountAsync<T>(string keyPattern)
        {
            var result = await RedisHelper.KeysAsync(keyPattern);
            return result.Length;
        }
        public async Task SetAsync<T>(string key, T item, TimeSpan expiration)
        {
            await RedisHelper.SetAsync(key, item, (int)expiration.TotalSeconds);
        }
        public async Task SetAsync<T>(string key, T item, int seconds)
        {
            await RedisHelper.SetAsync(key, item, seconds);
        }
        public async Task DelAsync(string key)
        {
            await RedisHelper.DelAsync(key);
        }
        public async Task SetHashAsync(string key, string field, object value)
        {
            await RedisHelper.HSetAsync(key, field, value);
        }
        public async Task DelHashAsnyc(string key, string[] fields)
        {
            await RedisHelper.HDelAsync(key, fields);
        }
        public async Task<T> GetHashAsync<T>(string key, string field)
        {
            var h = await RedisHelper.HGetAsync<T>(key, field);
            return h;
        }
        public async Task<bool> HashExsitAsync(string key, string field)
        {
            return await RedisHelper.HExistsAsync(key, field);
        }
        public async Task<Dictionary<string, T>> GetHashAllAsync<T>(string key)
        {
            return await RedisHelper.HGetAllAsync<T>(key);
        }
        public async Task<int> HIncrAsync(string key, string field, int num)
        {
            return (int)await RedisHelper.HIncrByAsync(key, field, num);
        }
        public async Task<string[]> GetHKeys()
        {
            return await RedisHelper.HKeysAsync("Mission:d5cf4823-27a2-4be9-be24-4797e06f989c:00124B00194B5004");
        }
        public async Task SetListAsync(string key, long index, object value)
        {
            await RedisHelper.LSetAsync(key, index, value);
        }
        public async Task RPushAsync(string key, object value)
        {
            await RedisHelper.RPushXAsync(key, value);
        }
        public async Task<T> LIndexAsync<T>(string key, int index)
        {
            return await RedisHelper.LIndexAsync<T>(key, index);
        }
        public T LIndex<T>(string key, int index)
        {
            return  RedisHelper.LIndex<T>(key, index);
        }
        public long LLen(string key)
        {
            return RedisHelper.LLen(key);
        }
    }
}

